$(document).ready(function () {
    userChange();
    $(".parent_nav li").mouseover(function () {
        var index = $(".parent_nav li").index($(this));
        var keyword = $(".parent_nav li a:eq("+index+")").attr("href");
        goodList(keyword,index);
        $(".right div").show();
    });
    $(".parent_nav li").mouseout(function(){
        $(".children ul").remove();
        $(".right div").hide();
    });

    var rotateImg=[];
    var wrapIndex = 0;
    var wrapSize = 0;
    $.ajax({
        url:"../rotation?method=getImg",
        type:"post",
        dataType:"json",
        success:function(data){
            $.each(data,function (index,item) {
                wrapSize=index+1;
                rotateImg.push(item.url);
            })
            $(".wrap img").attr("src",rotateImg[wrapIndex]);
            console.log(rotateImg);
        },error:function () {
            alert("系统异常或数据格式错误");
        }
    })
    setInterval(scroll,3000);
    function scroll() {
        wrapIndex++;
        if(wrapIndex==wrapSize){
            wrapIndex=0;
        }
        $(".wrap img").attr("src",rotateImg[wrapIndex]);
    }
    getSeckill();
})
function goodList(keyword,categoryId){
    $.ajax({
        "url":"../goods.do",
        "type":"POST",
        "data":{"method":"searchGoods","keyword":keyword,"category":categoryId+1,"state":1},
        "dataType":"json",
        "success":function(result){
            var i = 0;
            $.each(result,function (index,data) {
                var c = parseInt(i/6);
                if(i==24){
                    return;
                }
                if(i%6==0){
                    $(".children").append($("<ul></ul>"));
                }
                $(".children ul:eq("+c+")").append(printGoods(data));
                i++;
            });
        }

    })
}
function printGoods(goods){
    return $("<li>"+goods.name+"</li>");
}
function  userChange() {
    $.ajax({
        "url":"../userlogin.do",
        "type":"POST",
        "data":{"method":"review"},
        "dataType":"json",
        "success":function(result){
            nameChange(result);
        }
    })
}
function nameChange(user) {
    if(user.name=='none'){
        $(".login").text("登录");
        $(".login").attr("href","../loginuser.html");
    }else {
        $(".login").text(" "+user.name+"");
        $(".login").attr("href","user.html?id="+user.id+"");
    }
}
function getSeckill() {
    $.ajax({
        url:"../seckill?method=getSeckillList",
        type:"post",
        dataType:"json",
        success:function (data) {
            var str = "";
            $.each(data,function (index,item) {
                console.log(item)
                // str+="<img src='"+item.url+"'/>";
                if(index=="data"){
                    $.each(item,function (i,seckillVo) {
                        var id=seckillVo.id;
                        str+="<div style='float:left'>"
                        str+="<div><img width='100px' height='100px' src='"+seckillVo.url+"'/></div>"+"<a class='seckill_goods' style='color: red' id='"+id+"'>秒杀</a>";
                        str+="<p>商品名称："+seckillVo.goods_name+"</p>";
                        str+="<p>秒杀价：￥"+seckillVo.price+"</p>";
                        str+="</div>";
                    })
                }
            })
            $(".seckill").append(str);
        }
    })
    $(document).on("click",".seckill_goods",function (e) {
        e.preventDefault();
            var id = $(this).attr("id");
            var obj = $(this);
            alert(id);
            $.ajax({
                url:"/seckill",
                data:{"method":"seckill","seckill_id":id},
                dateType:"json",
                type:"post",
                success:function (result) {
                    if(result=="SUCCESS"){
                        alert("恭喜您，秒杀成功！")
                        obj.attr("class","");
                        obj.html("已抢购");
                    }else if (result!=""){
                        alert(result);
                    }else {
                        alert("请先登录！")
                        window.location.href="loginuser.html"
                    }
                },error:function () {
                     alert("系统异常")
                }
            })
    })
}